<?php
 class Wallpaper_model extends Model {
 	function Wallpaper_model()
 	{
 		parent::Model();
 	}
    // add categeory into database 
 	function addCategeory($catname,$parentid)
 	{
  	  $this->db->insert('wallpaper_categeory',array('catname' =>$catname ,
 	  'parentid'=> $parentid));
  	}
  	//getting info of categeories from database and saving 
  	//them in array options for dropdown list
  	function Categeory($parentid)
	{
	  $this->db->order_by('update_date','desc');
	  $this->db->where('parentid',$parentid);
	  $query = $this->db->get('wallpaper_categeory');
	  $cat = $query->result();
      if($parentid ==0){
      $options[0] = 'Root';
      }
      else{
      $options[0] = 'Select One';
      }
	  
	//sending root categeories to admin form
	  foreach($cat as $row)
        {
         $options[$row->id]= $row->catname;
        }
        
        return $options;
   	}	
 	
 	//extract all categery details for displaying   	
   function wallpaperview($parentid,$num,$offset)
    {
		//extracting root categeories from database
		$this->db->order_by('update_date','desc');
		$this->db->where('parentid',$parentid);
		$query = $this->db->get('wallpaper_categeory',$num, $offset);
		return $query;
	}
	//checking whether root or not
	function isrootcategeory($id)
	{
		$this->db->where('id',$id);
		$query = $this->db->get('wallpaper_categeory');
		$result = $query->result();
		
	if($result[0]->parentid == 0)
		{
			
	   return true;		
	   
		}
	else {
	
	   return false;
	}
	   
	}
	function deletecategeory($id)
 	{
 		$this->db->where('id',$id);
 		$this->db->delete('wallpaper_categeory');
 		//if it is paent wallpaper delete sub categeories also.
 		
 		//redirect('admin/wallpaper/wallpaperview/0');
  	}
  	function deletesubcategeory($id)
    {
 		$this->db->where('parentid',$id);
 		$this->db->delete('wallpaper_categeory');
 		//if it is paent wallpaper delete sub categeories also.
  	}
    function cat($id)
  	{
  		$this->db->where('id',$id);
 		$query = $this->db->get('wallpaper_categeory');
 		return $query;
  	}
 	// function to get all sub-categories in movies category
	
	function get_movies($cat = 'movies')
	{
		 $query = "SELECT a.catname,a.id FROM
 				 wallpaper_categeory a , wallpaper_categeory b 
 				 where a.parentid = b.id  and b.catname = '$cat' order by a.id desc";	 
 		$result = $this->db->query($query);
 		return $result->result();
	}
  	//rename,add image into database and folder assets	
 	function addImage($title,$parentid) 	{
 	   
 	   
}
	//fetching all sub categeories from database
    function subcat($uriseg)
	{
		$this->db->where('parentid',$uriseg);
		$query =$this->db->get('wallpaper_categeory');
		return $query;
	}
	//editwallpaper
 	function editcategeory($id,$catname)
 	{ 		
 		$this->db->where('id',$id);
 		$this->db->update('wallpaper_categeory',array('catname' => $catname));
 		
 	}
    //extract image details from adatabase 
    function image_view($parentid){
    	
    	$this->db->where('parentid',$parentid);
    	$query = $this->db->get('wallpaper_images');
    	return $query;
    	
    }
    //edit image form
     function imageedit($uriseg)
     {
     	$this->db->where('id',$uriseg);
 		$query=$this->db->get('wallpaper_images');
 		return $query->result();
     	   	
     }
     //edit image
    function editimage($id,$title)
    {
    	$this->db->where('id',$id);
 		$this->db->update('wallpaper_images',array('title' => $title));
 		
    }
    //delete image
    function deleteimage($id)
 	{ 		
 		$this->db->where('id',$id);
 		$this->db->delete('wallpaper_images');
  	}
  	
}
?>